﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using Core.Domain.Bases;

namespace TallyJ.EntityWrappers
{
  internal class Wrapper<TEntity> : INotifyCollectionChanged where TEntity : EntityBase
  {
    public Wrapper(IEnumerable<TEntity> source)
    {
      Items = new ObservableCollection<TEntity>(source);
      Items.CollectionChanged += ItemsOnCollectionChanged;
    }

    public ObservableCollection<TEntity> Items { get; set; }

    #region INotifyCollectionChanged Members

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    #endregion

    public virtual event PropertyChangedEventHandler PropertyChanged;

    private void ItemsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
    {
      // TODO: glittle 2009-04-12 - Needs attention
      switch (args.Action)
      {
        case NotifyCollectionChangedAction.Add:
          foreach (TEntity newItem in args.NewItems)
          {
            Items.Add(newItem);
          }
          break;
        case NotifyCollectionChangedAction.Remove:
          foreach (TEntity newItem in args.NewItems)
          {
            Items.Remove(newItem);
          }
          break;
        case NotifyCollectionChangedAction.Replace:
          break;
        case NotifyCollectionChangedAction.Move:
          break;
        case NotifyCollectionChangedAction.Reset:
          break;
        default:
          throw new ArgumentOutOfRangeException();
      }
    }

    protected void NotifyPropertyChanged(string propertyChanged)

    {
      if (PropertyChanged != null)

      {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyChanged));
      }
    }
  }
}